package com.siruier.boss.ui.activity.mall

import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.appbar.AppBarLayout
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.api.MALL_API_SERVICE
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.MallCategoryBean
import com.siruier.boss.bean.MallGoodsBean
import com.siruier.boss.common.GOODS_SPAN_COUNT
import com.siruier.boss.databinding.ActivityStoreBinding
import com.siruier.boss.ui.adapter.GoodsAdapter
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.ext.showLoadingCallback
import com.siruier.boss.ui.ext.tabData
import com.siruier.boss.ui.helper.RefreshHelper
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper
import com.siruier.boss.ui.listener.OnTabSelectedListener
import com.siruier.boss.ui.utils.Utils

class StoreActivity : BaseActivity<ActivityStoreBinding>() {
    private var mMallCategory: MutableList<MallCategoryBean>? = null
    private lateinit var rootLoadService: LoadService<*>
    private lateinit var mRefreshHelper: RefreshHelper<MallGoodsBean>
    private lateinit var mGoodsAdapter: GoodsAdapter
    private var mLayoutManager: GridLayoutManager? = null
    override fun initView() {
        bindViewClick(vb.ivToUp)

        rootLoadService = LoadSirHelper.register(vb.rootView, listener = ::loadData)
        rootLoadService.showLoadingCallback()

        mGoodsAdapter = GoodsAdapter()
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.rvGoods, mGoodsAdapter, listener = ::goodsList)
        mLayoutManager = GridLayoutManager(this, GOODS_SPAN_COUNT)
        vb.rvGoods.layoutManager = mLayoutManager

        vb.tabLayout.addOnTabSelectedListener(OnTabSelectedListener {
            mLayoutManager?.scrollToPositionWithOffset(0, 0)
            mRefreshHelper.onRefresh()
        })

        vb.searchView.clearListener = ::goodsList

        loadData()
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.ivToUp -> {
                mLayoutManager?.scrollToPositionWithOffset(0, 0)
            }
        }
    }

    private fun loadData() {
        launchUI(
            {
                mMallCategory = mallApi.category().toData()
                vb.tabLayout.removeAllTabs()
                vb.tabLayout.tabData {
                    tab { "全部" }
                    mMallCategory?.forEach {
                        tab { it.name }
                    }
                }
                loadGoods()
            }, ApiObserver(
                showMsg = false,
                loadService = rootLoadService,
                apiObserver = mRefreshHelper.handApiObserver()
            )
        )
    }

    private fun goodsList() {
        Utils.hideSoftInput(this)
        launchUI({ loadGoods() }, mRefreshHelper.handApiObserver())
    }

    private suspend fun loadGoods(): MutableList<MallGoodsBean>? {
        val categoryId = if (vb.tabLayout.selectedTabPosition == 0) {
            null
        } else {
            mMallCategory?.get(vb.tabLayout.selectedTabPosition - 1)?.id
        }
        return MALL_API_SERVICE.goodsList(vb.searchView.text, categoryId, mRefreshHelper.page).toData()?.records
    }
}